package com.zrrd.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;

import com.zrrd.reggie.bean.AddressBook;
import com.zrrd.reggie.common.R;
import com.zrrd.reggie.common.ThreadLocalUtil;
import com.zrrd.reggie.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    //添加收货地址
    @PostMapping
    public R<String> add(@RequestBody AddressBook addressBook){
        //获取用户id
        Long userId = ThreadLocalUtil.getCurrentId();
        addressBook.setUserId(userId);
        QueryWrapper<AddressBook> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<AddressBook> list = addressBookService.list(wrapper);
        if(list==null){
            addressBook.setIsDefault(1);
        }
        addressBookService.save(addressBook);
        return R.success("添加收货地址成功");

    }

    //展示收货地址
    @GetMapping("/list")
    public R<List<AddressBook>> list(){
        QueryWrapper<AddressBook> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",ThreadLocalUtil.getCurrentId());
        wrapper.orderByDesc("create_time");
        List<AddressBook> list = addressBookService.list(wrapper);
        return R.success(list);
    }

    //修改默认收货地址
    @PutMapping("/default")
    public R<String> updateAddress(@RequestBody AddressBook addressBook){
        //将当前用户的所有地址都更新为0
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId,ThreadLocalUtil.getCurrentId());
        wrapper.set(AddressBook::getIsDefault,0);
        addressBookService.update(wrapper);
        //将传入的地址设置为默认
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success("设置默认地址成功");
    }

    //获取默认收货地址
    @GetMapping("/default")
    public R<AddressBook> getAddress(){
        Long userId = ThreadLocalUtil.getCurrentId();
        QueryWrapper<AddressBook> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("is_default",1);
        AddressBook one = addressBookService.getOne(wrapper);
        return R.success(one);
    }

    //根据id获取收货地址信息
    @GetMapping("/{id}")
    public R<AddressBook> getAddressBook(@PathVariable("id") Long id){
        QueryWrapper<AddressBook> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        AddressBook one = addressBookService.getOne(wrapper);
        return R.success(one);
    }

    //修改地址信息
    @PutMapping
    public R<String> updateAddressBook(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("修改收货地址成功");
    }

    //删除地址
    @DeleteMapping
    public R<String> deleteAddressBook(Long ids){
        QueryWrapper<AddressBook> wrapper = new QueryWrapper<>();
        wrapper.eq("id",ids);
        addressBookService.remove(wrapper);
        return R.success("删除成功");
    }



}
